# @Time : 2023/1/14
# @Author : SDAJAVA

import requests
import re
import json

url = 'https://www.acfun.cn/v/ac35205510'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

response = requests.get(url=url, headers=headers)

html_data = re.findall('window.pageInfo = window.videoInfo = (.*?);', response.text)[0]
json_data = json.loads(html_data)

title = json_data['title']
video_info = json_data['currentVideoInfo']['ksPlayJson']

m3u8_url = json.loads(video_info)['adaptationSet'][0]['representation'][0]['backupUrl'][0]

m3u8_data = requests.get(url=m3u8_url, headers=headers).text

m3u8_data = re.sub('#E.*', '', m3u8_data).split()


for index in m3u8_data:
    ts_url = 'https://tx-safety-video.acfun.cn/mediacloud/acfun/acfun_video/' + index
    ts_content = requests.get(url=ts_url, headers=headers).content

    with open('fengjing\\' + title + '.mp4', mode='ab') as fp:
        fp.write(ts_content)
